package com.heima.bos.web.action.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.junit.Test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	//属性注入file上传文件
	private File deploy;
	

	/**
	 * 查询最新版本的流程定义列表
	 */
	public String list(){
		
		ProcessDefinitionQuery query = processEngine.getRepositoryService()
															.createProcessDefinitionQuery();
		//过滤 获得最新版本
		query.latestVersion();
		//获取查询列表
		List<ProcessDefinition> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	/**
	 * 发布新流程
	 * @return
	 * @throws FileNotFoundException
	 */
	public String deploy() throws FileNotFoundException{
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		InputStream in = new FileInputStream(deploy);
		deploymentBuilder.addZipInputStream(new ZipInputStream(in));
		deploymentBuilder.deploy();
		return "toList";
	}
	/**
	 * 查看流程图
	 * @param deploy
	 */
	//属性注入当前流程图id
	private String id;
	public String viewpng(){
		InputStream pngStream = processEngine.getRepositoryService().getProcessDiagram(id);
		ActionContext.getContext().getValueStack().set("pngStream", pngStream);
		return "viewpng";
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setDeploy(File deploy) {
		this.deploy = deploy;
	}
}
